$(function () {
    $("#jqGrid").Grid({
        url: '../maintaplanchangerecords/list',
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '模具维护计划ID', name: 'modelMaintaPlanId', index: 'MODEL_MAINTA_PLAN_ID', hidden: true},
            {label: '维护计划名称', name: 'modelMaintaPlanName', index: 'MODEL_MAINTA_PLAN_NAME', width: 80},
            {label: '审批时间', name: 'approvalTime', index: 'APPROVAL_TIME', width: 80,formatter:function (value) {
                    return transDate(value, "yyyy-MM-dd hh:mm");
                }},
            {label: '审批状态', name: 'approvalStatus', index: 'APPROVAL_STATUS', width: 80,formatter:function (value) {
                    return sysEnums.APPVAL_STATUS[value];
                }},
            {label: '审批人', name: 'approver', index: 'APPROVER', width: 80},
            // {label: '变更原因', name: 'changeReason', index: 'CHANGE_REASON', width: 80},
            // {label: '工单执行人', name: 'orderExecutor', index: 'ORDER_EXECUTOR', width: 80},
            {label: '原计划时间', name: 'initiationTime', index: 'INITIATION_TIME', width: 80,
                formatter:function (value) {
                    return transDate(value, "yyyy-MM-dd hh:mm");
                }},
            {label: '申请延期时间', name: 'newPlanTime', index: 'NEW_PLAN_TIME', width: 80,
                formatter:function (value) {
                    return transDate(value, "yyyy-MM-dd hh:mm");
                }},
            // {label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80},
            {label: '发起人', name: 'initiator', index: 'INITIATOR', width: 80},
            {label: '所需资源', name: 'resources', index: 'RESOURCES', width: 80},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        repairUserList:{},
        maintaPlanChangeRecords: {
            delFlag: '',
            createTime: '',
            creater: '',
            remark: '',
            approvalTime: '',
            approvalStatus: '',
            approver: '',
            changeReason: '',
            orderExecutor: '',
            initiationTime: '',
            initiator: '',
            modelMaintaPlanId: '',
            id: '',
            status:'',
            planEndDate:'',
            modelMaintaPlanName:'',
            newPlanTime:''
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.maintaPlanChangeRecords = {
                delFlag: '',
                createTime: '',
                creater: '',
                remark: '',
                approvalTime: '',
                approvalStatus: '',
                approver: '',
                changeReason: '',
                orderExecutor: '',
                initiationTime: '',
                initiator: '',
                modelMaintaPlanId: '',
                id: '',
                modelMaintaPlanName:''
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.maintaPlanChangeRecords.id == (null || '') ? "../maintaplanchangerecords/save" : "../maintaplanchangerecords/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.maintaPlanChangeRecords),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../maintaplanchangerecords/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../maintaplanchangerecords/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.maintaPlanChangeRecords = r.maintaPlanChangeRecords;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        apply:function () {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            var date = getSelectedRowData("#jqGrid");
            var status = date.approvalStatus;
            if('通过'==status){
                alert("当前任务已完成",function () {
                    return;
                });
            }else{
                vm.maintaPlanChangeRecords.modelMaintaPlanId=date.modelMaintaPlanId;
                vm.maintaPlanChangeRecords.newPlanTime=date.newPlanTime;
                vm.maintaPlanChangeRecords.id=ids[0];
                vm.getAssign()
                openWindow({
                    title:'审核',
                    content: jQuery("#applyLayer"),
                    area: ['500px', '350px'],
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        Ajax.request({
                            url:"../maintaplanchangerecords/updateApproval",
                            params: JSON.stringify(vm.maintaPlanChangeRecords),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    layer.closeAll();
                                    vm.reload();
                                });
                            }
                        })
                    }
                });
            }
        },
        change:function () {

        }
        ,getAssign: function () {
            //获取当前登录部门的维修组人员信息
            Ajax.request({
                url: '../modulemaintainplan/repairUser',
                async: false,
                successCallback: function (r) {
                    vm.repairUserList = r.list;
                }
            })
        },
    }
});